package com.newvpn.fastbest.zenith.presenter;

import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.newvpn.fastbest.zenith.VPNApp;
//选择v、上一次v、当前连接v
public  class VPNDetailInfo {
    public String nodeId;
    public String countryName;
    public String iconUrl;
    public String serverId;
    public String serverName;
    public String serverIp;
    public int serverPort;

    public String sessionId;
    public int addTime;

    public long testTime = Long.MAX_VALUE;
    public long startTime;


    public long getPingTime() {
        return testTime;
    }

    public VPNDetailInfo withPingTime(long pingTime) {
        testTime = pingTime;
        return VPNDetailInfo.this;
    }

    public int getLeftTime(){
        return addTime - (int)((System.currentTimeMillis() - startTime)/1000);
    }



    public static class VPNPrefs {

        private static final String PREFS_NAME = "vpnPrefs";
        private static final String KEY_VPN_INFO = "vpnInfo";

        public  static void saveVPNDetailInfo(VPNDetailInfo vpnDetailInfo) {
            Context context = VPNApp.getBase();
            SharedPreferences sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();

            // 使用Gson将对象转换为JSON字符串
            Gson gson = new Gson();
            String json = gson.toJson(vpnDetailInfo);
            // 存储JSON字符串
            editor.putString(KEY_VPN_INFO, json);
            editor.apply();
        }

        public  static VPNDetailInfo getVPNDetailInfo() {
            Context context = VPNApp.getBase();
            SharedPreferences sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            // 读取JSON字符串
            String json = sharedPreferences.getString(KEY_VPN_INFO, null);
            if (json != null) {
                Gson gson = new Gson();
                return gson.fromJson(json, VPNDetailInfo.class);
            }
            return null;
        }
    }
}